<?php


//echo is_dir("../config");die();
//require_once("..\..\index.php");
//require_once("..\models\user.php");
//require_once '..\..\system\core\Model.php';
class Restaurant extends CI_Controller {

	function index($id)
	{
		if(!isset($id))
		{
			$id = 2;
		}
		$this->homePage($id);
	}
	
	
	function test($pagename)
	{
		$pagename = 'restaurant/'.$pagename;
		//$this->load->view('restaurant/restaurant', array());
		$this->load->view($pagename, array());
	}
	/**
	 * 
	 * 店铺主页函数。输入店铺id，返回restaurant信息，category信息，food信息,cart信息
	 * @param int $id 店铺id
	 */
	function homePage($id)
	{
		//echo 'HomePage '.$id;
		if(!isset($id))
		{
			echo "need ID";return;
		}
		
		//加载restaurant信息
        $this->load->model('restaurant_model');
        $data = array();
		$restaurantInfo = $this->restaurant_model->load($id);
		
		//加载food信息
        $this->load->model('food_model');
		$result = $this->food_model->getFoodByRestaurantid($id);
		
		//按category显示food
		$categoryFoodArray = array();
		
		//先建立categroyidArray，随后到数据库中取所有category信息
		$categoryIDArray = array();
	
		foreach($result as $food)
		{			
			if(!empty($food->categoryId))
			{
				$categoryId = $food->categoryId;
				$categoryIDArray [] = $categoryId;
			}
			else {
				$categoryId = -1;
			}
		
			
			if(!isset($categoryFoodArray[$categoryId]))
			{
				$categoryFoodArray[$categoryId] = array();
			}
			$categoryFoodArray[$categoryId][] = $food;
		}
		//取category信息
        $this->load->model('category_model');
		$categoryArray = $this->category_model->getCategoryByIdarray($categoryIDArray);
		
		//填充categoryMap
		$categoryMap = array();
		$sortCategoryFoodArray = array();
		foreach ($categoryArray as $category)
		{
			$categoryMap[$category->id] = $category;
			$sortCategoryFoodArray[$category->id] = $categoryFoodArray[$category->id];
		}
		
		//填充crumb
		$data['location'] = $this->getLocationInfo();
		
		//显现主页
		$data['restaurant'] = $restaurantInfo;
		$data['categoryFoodArray'] = $sortCategoryFoodArray;
		$data['categoryMap'] = $categoryMap;
		$data['title'] = $restaurantInfo['name'];
		//$cart = $this->session->userdata('cart');
		
		//进入不同店铺时，要清空其他店铺的cart(目前始终保证只存一个店铺cart。如果存多个店铺cart涉及到购物车，下单页等的一致统一。需要在通盘设计)
		$cart = $this->clearCartWhenChangeRestaurant($id);
		$data['cart'] = $cart;
		$this->load->view('restaurant/restaurantHome', $data);
	}

	private function clearCartWhenChangeRestaurant($restaurantID){
		$this->load->model('cart_model');
		$cart = $this->cart_model->clearCartWhenChangeRestaurant($restaurantID);
		
		return $cart;
	}
	/**
	 * 
	 * 店铺主页的”餐厅信息“按钮
	 */
	function info($id)
	{
		if(!isset($id))
		{
			echo "need ID";return;
		}
		
        $data = array();
		//填充crumb
		$data['location'] = $this->getLocationInfo();
		//加载restaurant信息
        $this->load->model('restaurant_model');
		$restaurantInfo = $this->restaurant_model->load($id);
		$data['restaurant'] = $restaurantInfo;
		$data['title'] = $restaurantInfo['name'];
		$this->load->view('restaurant/restaurantInfo', $data);
		
	}
	
	function getLocationInfo(){
		$entryId = get_cookie('entry');
		$this->load->model('location_model');
		return $this->location_model->findById($entryId);
	}
	function addNewRestaurants()
	{
		
		echo 'Start add a new restaurant <br/>';
		$date['name'] = "songfanla"; 
        $date['address']= "中科院";
        $date['coordinate']= "user@songfanla.com";
		$date['description'] = "外卖"; 
        $date['homepage']= "www.songfan.la";
        $date['notice']= "最近新上砂锅，欢迎品尝";
		$date['serviceTime'] = "早10：00-10：00送饭"; 
        $date['promotion']= "1";
        $date['compensate']= "no";
		$date['phone'] = "88888888";
        $date['picture']= "/default.png";
        $date['owner']= 1;
		$date['ownerName'] = "songfanla.name";
        $date['ownerMobile']= "356782256";
        $date['ownerPhone']= "123355677";
		$date['ownerQQ'] = "10000";
        $date['status']= "1";
        $date['regTime']= "2011-04-01";
        $this->load->model('restaurant_model');
        $this->restaurant_model->insert_entry($date);
		echo 'End add a new restaurant <br/>';
	}
	
	function favorite($restaurantId){
		
	}
}